﻿module Sustainalytics.News {

    import Common = Sustainalytics.Common;

    // Home controller
    export class NewsListController {
        public newsTopList: any[] = new Array;
        // order by column
        public newsOrderByCol: string;
        // order by direction
        public newsOrderByColAsc: boolean;
        public lastSkip: number;
        private searchResultsElement;
        private accountId: string;
        private userId: string;

        //Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private NewsService: NewsService,
            private notificationFactory: Common.INotificationFactory) {

            var ctx = this;

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.accountId = ctx.$rootScope['userInfo'].accountId;
            ctx.userId = ctx.$rootScope['userInfo'].userId;

            $scope['newsList'] = ctx;
            ctx.newsOrderByCol = 'DateCreated';
            ctx.newsOrderByColAsc = false;
            ctx.newsTopList = new Array();
            ctx.lastSkip = 0;
            ctx.getNews();
            ctx.findInDom();
            ctx.addItemsOnScroll();
        }

        public getNews(): ng.IPromise<{}> {
            var ctx = this;
            ctx.$rootScope['loading'] = true;
            ctx.newsOrderByCol = 'DateCreated';
            ctx.newsOrderByColAsc = false;

            var promiseGet: ng.IPromise<NewsDto[]> = ctx.NewsService.getNewsList(0, 10, [ctx.newsOrderByCol], ctx.newsOrderByColAsc, true, ctx.accountId);

            //on success
            promiseGet
                .then(function (lst: any[]) {
                    if (lst) {
                        $(lst).each(function () {
                            ctx.newsTopList.push(this);
                        });
                    }
                })
                ['finally'](function () {
                    ctx.lastSkip += 10;
                    ctx.$rootScope['loading'] = false;
                });
            return promiseGet;
        }

        private findInDom() {
            var ctx = this;
            ctx.searchResultsElement = $('#newsContainer');// ctx.element.find('#newsContainer');
        }

        private addItemsOnScroll() {
            var ctx = this;

            // handle scroll
            ctx.searchResultsElement.on('scroll', function (ev) {
                // get scrollbar element
                var target = $(ev.target);

                // check if it reached the max scroll size and go to next page
                if (target[0].scrollHeight == (target.scrollTop() + target.outerHeight())) {
                    ctx.$scope.$apply(function () {

                        ctx.$rootScope['loading'] = true;
                        ctx.newsOrderByCol = 'DateCreated';
                        ctx.newsOrderByColAsc = false;

                        var promiseGet: ng.IPromise<NewsDto[]> = ctx.NewsService.getNewsList(ctx.lastSkip, 10, [ctx.newsOrderByCol], ctx.newsOrderByColAsc, true, ctx.accountId);

                        //on success
                        promiseGet
                            .then(function (lst: any[]) {
                                if (lst) {
                                    $(lst).each(function () {
                                        ctx.newsTopList.push(this);
                                    });
                                }
                            })
                            ['finally'](function () {
                                ctx.lastSkip += 10;
                                ctx.$rootScope['loading'] = false;
                            });
                        return promiseGet;
                    });
                }
            });
        }
    }

    // register controller 
    angular.module('Sustainalytics.News').controller('NewsListController', function ($rootScope: ng.IScope, $scope: ng.IScope, $q: ng.IQService, $location: ng.ILocationService, NewsService, notificationFactory: Common.INotificationFactory) {
        var ctrl = new Sustainalytics.News.NewsListController($rootScope, $scope, $q, $location, NewsService, notificationFactory);
        return ctrl;
    });
} 